package cn.akwangl.concurrency.web.model.order.pojo;

import cn.akwangl.concurrency.web.common.enums.OrderStatusEnum;
import cn.akwangl.concurrency.web.common.enums.YesOrNO;
import cn.akwangl.concurrency.web.model.items.pojo.ItemsEntity;
import cn.akwangl.concurrency.web.model.items.pojo.ItemsImgEntity;
import cn.akwangl.concurrency.web.model.items.pojo.ItemsSpecEntity;
import cn.akwangl.concurrency.web.model.user.pojo.UserAddressEntity;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 订单工厂.
 *
 * @author 阿坤同学
 * @date 2022-11-23 9:08
 */
public class OrderFactory {

    /**
     * 支付回调地址
     */
    public static final String PAY_RETURN_URL = "";


    /**
     * 创建订单明细.
     *
     * @param buyCounts       购买数
     * @param orderId         订单Id
     * @param itemsEntity     商品实体
     * @param itemsSpecEntity 商品规格实体
     * @param itemsImgEntity  商品图片实体
     * @return 商品明细
     * @author 阿坤同学
     * @date 2022-11-23 9:38
     */
    public static OrderItemsEntity createOrderItemsEntity(Integer buyCounts,
                                                          String orderId,
                                                          ItemsEntity itemsEntity,
                                                          ItemsSpecEntity itemsSpecEntity,
                                                          ItemsImgEntity itemsImgEntity) {
        OrderItemsEntity orderItems = new OrderItemsEntity();
        orderItems.setOrderId(orderId);
        orderItems.setItemId(itemsEntity.getId());
        orderItems.setItemImg(itemsImgEntity.getUrl());
        orderItems.setItemName(itemsEntity.getItemName());
        orderItems.setItemSpecId(itemsSpecEntity.getId());
        orderItems.setItemSpecName(itemsSpecEntity.getName());
        orderItems.setPrice(itemsSpecEntity.getPriceDiscount());
        orderItems.setBuyCounts(buyCounts);
        return orderItems;
    }

    /**
     * 创建订单实体.
     *
     * @param orderId           订单Id
     * @param postAmount        邮费
     * @param totalAmount       总费用
     * @param realPayAmount     实际费用
     * @param submitOrderDto    订单dto
     * @param userAddressEntity 用户地址实体
     * @return 订单实体
     * @author 阿坤同学
     * @date 2022-11-23 9:47
     */
    public static OrdersEntity createOrderEntity(String orderId,
                                                 Integer postAmount,
                                                 Integer totalAmount,
                                                 Integer realPayAmount,
                                                 SubmitOrderDto submitOrderDto,
                                                 UserAddressEntity userAddressEntity) {
        String receiverAddress = userAddressEntity.getProvince() + userAddressEntity.getCity() + userAddressEntity.getDetail() + userAddressEntity.getDetail();
        OrdersEntity ordersEntity = new OrdersEntity();
        ordersEntity.setId(orderId);
        ordersEntity.setUserId(submitOrderDto.getUserId());
        ordersEntity.setReceiverName(userAddressEntity.getReceiver());
        ordersEntity.setReceiverMobile(userAddressEntity.getMobile());
        ordersEntity.setReceiverAddress(receiverAddress);
        ordersEntity.setTotalAmount(totalAmount);
        ordersEntity.setRealPayAmount(realPayAmount);
        ordersEntity.setPostAmount(postAmount);
        ordersEntity.setPayMethod(submitOrderDto.getPayMethod());
        ordersEntity.setLeftMsg(submitOrderDto.getLeftMsg());
        ordersEntity.setIsComment(YesOrNO.NO.type);
        ordersEntity.setIsDelete(YesOrNO.NO.type);
        ordersEntity.setCreatedTime(LocalDateTime.now());
        ordersEntity.setUpdatedTime(LocalDateTime.now());
        return ordersEntity;
    }

    /**
     * 创建订单状态
     *
     * @param ordersEntity 订单实体
     * @return 订单状态
     * @author 阿坤同学
     * @date 2022-11-23 9:47
     */
    public static OrderStatusEntity createOrderStatusEntity(OrdersEntity ordersEntity) {
        OrderStatusEntity orderStatusEntity = new OrderStatusEntity();
        orderStatusEntity.setOrderId(ordersEntity.getId());
        orderStatusEntity.setOrderStatus(OrderStatusEnum.WAIT_PAY.type);
        orderStatusEntity.setCreatedTime(LocalDateTime.now());
        return orderStatusEntity;
    }

    /**
     * 创建商户信息.
     *
     * @param ordersEntity 订单实体
     * @return 商户VO
     * @author 阿坤同学
     * @date 2022-11-23 9:09
     */
    public static MerchantOrdersVo createMerchantOrdersVo(OrdersEntity ordersEntity) {
        MerchantOrdersVo merchantOrdersVo = new MerchantOrdersVo();
        merchantOrdersVo.setMerchantOrderId(ordersEntity.getId());
        merchantOrdersVo.setMerchantUserId(ordersEntity.getUserId());
        merchantOrdersVo.setAmount(ordersEntity.getRealPayAmount());
        merchantOrdersVo.setPayMethod(ordersEntity.getRealPayAmount());
        merchantOrdersVo.setReturnUrl(PAY_RETURN_URL);
        return merchantOrdersVo;
    }

    /**
     * 创建订单VO.
     *
     * @param orderId          订单Id
     * @param shopCarts        最新购物车信息
     * @param merchantOrdersVo 商户信息
     * @return 订单VO
     * @author 阿坤同学
     * @date 2022-11-23 10:02
     */
    public static OrderVo createOrderVo(String orderId,
                                        List<ShopCart> shopCarts,
                                        MerchantOrdersVo merchantOrdersVo) {
        OrderVo orderVo = new OrderVo();
        orderVo.setOrderId(orderId);
        orderVo.setMerchantOrdersVo(merchantOrdersVo);
        orderVo.setNowShopCartList(shopCarts);
        return orderVo;
    }


}
